package cn.zlg.www.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.fragment.app.Fragment;


import com.sany.canwifi.R;

import cn.zlg.www.ErrData;
import cn.zlg.www.list.ErrListViewAdapter;
import cn.zlg.www.list.FrmListViewAdapter;
import cn.zlg.www.main.ItemData;

public class DisplayFragment extends Fragment {
	public static final String TAG="DisplayFragment";
	private ListView mFrmListView, mErrListView;
	private FrmListViewAdapter mFrmListAdapter;
	private ErrListViewAdapter mErrListAdapter;
	private RecvHandle mHandler;
	private boolean mCreated = false;
	private int mMode = FrmListViewAdapter.MODE_SORT;
	private View mView;

	public static final int MSG_RECV = 0;
	public static final int MSG_SEND = 1;
	public static final int ERR_DATA = 2;
	public static final int MSG_CLEAR = 3;

	public static final String LISTCHOICENAME = "listchoice";
	public static final String LISTRECV = "listrecv";
	public static final String LISTSEND = "listsend";
	public static final String LISTERR = "listerr";

	public DisplayFragment() {
		super();
		mHandler = new RecvHandle();
		mFrmListAdapter = new FrmListViewAdapter(mMode);
		mErrListAdapter = new ErrListViewAdapter();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.displaylayout, null);
		if (null == mFrmListView) {
			mFrmListAdapter.setLayoutInflater(inflater);
		}
		mFrmListView = (ListView) mView.findViewById(R.id.listViewRecv);
		if (null == mErrListView) {
			mErrListAdapter.setLayoutInflater(inflater);
		}
		mErrListView = (ListView) mView.findViewById(R.id.listViewErr);

		mCreated = true;
		return mView;
	}

	public void dealItem(int nType, Object obj) {
		switch (nType) {
			case MSG_RECV: {
				Message msg = new Message();
				msg.obj = obj;
				msg.what = MSG_RECV;
				mHandler.sendMessage(msg);
				break;
			}
			case MSG_SEND: {
				Message msg = new Message();
				msg.obj = obj;
				msg.what = MSG_SEND;
				mHandler.sendMessage(msg);
				break;
			}
			case ERR_DATA: {
				Message msg = new Message();
				msg.obj = obj;
				msg.what = ERR_DATA;
				mHandler.sendMessage(msg);
				break;
			}
			case MSG_CLEAR: {
				Message msg = new Message();
				msg.what = MSG_CLEAR;
				mHandler.sendMessage(msg);
				break;
			}
		}
	}

	@SuppressLint("HandlerLeak")
	private class RecvHandle extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Log.e(TAG, "handleMessage: "+msg.what );
			switch (msg.what) {
				case MSG_RECV: {
					ItemData itemData = (ItemData) msg.obj;
					itemData.bRecv = true;
					mFrmListAdapter.add(itemData);
					refreshRecvList();
					break;
				}
				case MSG_SEND: {
					ItemData itemData = (ItemData) msg.obj;
					itemData.bRecv = false;
					mFrmListAdapter.add(itemData);
					refreshRecvList();
					break;
				}
				case ERR_DATA: {
					mErrListAdapter.add((ErrData) msg.obj);
					refreshErrList();
					break;
				}
				case MSG_CLEAR: {
					mFrmListAdapter.clear();
					mErrListAdapter.clear();
					refreshRecvList();
					refreshErrList();
					break;
				}
				default:
					break;
			}
		}
	}

	public void refreshRecvList() {
		if (mCreated) {
			mFrmListAdapter.notifyDataSetChanged();
			mFrmListView.setAdapter(mFrmListAdapter);
		}
	}

	public void refreshErrList() {
		// TODO Auto-generated method stub
		if (mCreated) {
			mErrListAdapter.notifyDataSetChanged();
			mErrListView.setAdapter(mErrListAdapter);
		}
	}

	public void setListAdapterEmpty() {
		if (mFrmListView != null) {
			mFrmListView.setAdapter(null);
		}
		if (mErrListView != null) {
			mErrListView.setAdapter(null);
		}
	}

	public void setListAdapter() {
		if (mFrmListView != null) {
			mFrmListView.setAdapter(mFrmListAdapter);
			refreshRecvList();
		}
		if (mErrListView != null) {
			mErrListView.setAdapter(mErrListAdapter);
			refreshErrList();
		}
	}
}
